#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <semaphore.h>
#define NUM 5

int queue[NUM];
sem_t blank_number,product_number;

void* producer(void* arg)
{
    int i=0;
    while(1)
    {
        sem_wait(&blank_number);
        queue[i] = rand() %1000 +1;
        printf("-------Produce---%d\n",queue[i]);
        sem_post(&product_number);

        i = (i+1)%NUM;
        sleep(rand()%3);
    }
}
void* consumers(void* arg)
{
    int i =0;
    while(1)
    {
        sem_wait(&product_number);
        printf("~~~consume the %d product\n",queue[i]);
        queue[i] = 0;
        sem_post(&blank_number);
        i = (i+1)%NUM;
        sleep(rand()%3);
    }
}

int main_01()
{
    srand(time(NULL));
    pthread_t pid,cid;

    sem_init(&blank_number,0,NUM);
    sem_init(&product_number,0,0);

    pthread_create(&pid,NULL,producer,NULL);
    pthread_create(&cid,NULL,consumers,NULL);

    pthread_join(pid,NULL);
    pthread_join(cid,NULL);

    sem_destroy(&blank_number);
    sem_destroy(&product_number);


    return 0;

    //QCoreApplication a(argc, argv);

    //return a.exec();
}
